home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15080 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.6 KB  |  54 lines

  1. Path: news.iastate.edu!mlrivas
  2. From: mlrivas@iastate.edu (Matthew L Rivas)
  3. Newsgroups: comp.lang.c++
  4. Subject: Overiding [] for C++ matrix class
  5. Date: 3 Apr 1996 15:54:23 GMT
  6. Organization: Iowa State University, Ames, Iowa USA
  7. Message-ID: <4ju6vf$kop@news.iastate.edu>
  8. NNTP-Posting-Host: burn.cce.iastate.edu
  9. Originator: mlrivas@burn.cce.iastate.edu
  10.  
  11.  
  12. I'm attempting to write a simple matrix class which will
  13. allow me to use the `[]' to access the matrix coefficients.
  14. The method I use is the follwing:
  15.  
  16.     virtual double * operator[](short r) {return coef[r];}
  17.  
  18. which I found in a book. I derived a symmetric matrix class
  19. from this base. In the symmetric class, I only store the
  20. upper triangular coefficients to save memory (my work
  21. is extrememly memory intensive so this is necessary). I have an
  22. access function which checks the called for position and
  23. returns the correct coefficient, and it looks like:
  24.  
  25.     double symmatrix::getValue(short i,short j)
  26.     {
  27.         if (j >= i) return coeff[i][j];
  28.         else return coeff[j][i];
  29.     }
  30.  
  31. I would like to have the same access function method, i.e., using `[]',
  32. for the symmetric class as well.
  33.  
  34. I have two questions:
  35.  
  36.   1) Is there a way to use `[]' for the symmetric class?
  37.  
  38.   2) How exactly does the first routine work? I've never been sure
  39.      and would really like to know.
  40.  
  41. Any help would be appreciated. I do know about overloading the
  42. function calling operators `()' so that
  43.  
  44.     Matrix A(n,n);
  45.     A(i,j) = 1.0;
  46.     x = A(i,j);
  47.  
  48. which is the same as the fortran calling convention. I don't really
  49. want to implement it this way and would rather stick with the C++
  50. standard if I can.
  51.  
  52. Matthew Rivas
  53. mlrivas@iastate.edu
  54.